//  Created by <Omar MALAGOUEN>

//  Copyright 2011 All rights reserved.
//

/*
 * Various utilities for command line tools
 * Copyright (c) 2000-2010 Omar Malagouen
 *
 * This file is part of Kakola.
 *
 * Kakola is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Kakola is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Kakola; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef kakola_H
#define kakola_H

#include <QDesktopServices>
#include <QtGui/QMainWindow>
#include <QMessageBox>
#include <QTranslator>
#include <QDockWidget>
#include <QUrl>
#include <QFileDialog>
#include <QProcess>
#include <QThread>
#include <QTextStream>
#include <QFile>
#include <QStandardItemModel>
#include <QDoubleSpinBox>
#include <QTableWidgetItem>
#include <QItemDelegate>
#include <QProgressBar>
#include <QTime>
#include <QDropEvent>
#include <QProgressDialog>
#include <QDir>
#include <QScrollBar>
#include <QTimer>
#include <QAction>
#include <QToolTip>
#include <QSystemTrayIcon>
#include <QTextBrowser>
#include <QSettings>
#include <QInputDialog>
#include <QtXml/QDomDocument>

#include "common.h"
#include "cvideofile.h"
#include "cripdvd.h"
#include "coptions.h"
#include "cdialogeventname.h"
#include "log.h"
#include "cnewprofile.h"
#include "cquitdialog.h"
#include "qdownloader.h"


class QDragEnterEvent;
class QDropEvent;

namespace Ui
{
    class kakolaClass;
}


class kakola : public QMainWindow
{
    Q_OBJECT

protected:
    void closeEvent(QCloseEvent *event);
    void resizeEvent(QResizeEvent *e);
    void changeEvent ( QEvent *event );



public:
    kakola(QWidget *parent = 0);
    ~kakola();
    void readSettings();
    void writeSettings();
    QProcess    m_Process[MAXCPU];
    QListWidget *kakolaListProfileInCombo;
    QIcon windowIcon,okIcon,nokIcon,goIcon;
    QIcon welcomeIcon;
    QString donateHash;
    int session;
    double intElapsedSinceEncodingStarted;
    QProcess    m_ProcessInfo;
    bool stopWorking;
    int indiceOfInProgress[MAXCPU];
    int counter;
    QString *popupEventName;
    QHash<QString,QString> *hashEventFolderName;
    int counter2[MAXCPU];
    QtMsgType msgType;
    QSettings *settings;
    QString notOptimized;
    int refresh;
    int numberOfDone;
    int numberOfFileInQueue;
    QTime elapsedSinceEncodingStarted;
    QString popupSettingIntrouvable;
    QList<CVideoFile*> listCVideoFile;
    QList<CVideoFile*> listCVideoFileFromXmlFile;
    QString selectedFileName;
    bool started;
    QTimer *timer;
    int tryToLoadOptionsAgain;
    QString programEncoder;
    QProgressBar* progressBarTableWidget[MAXCPU];
    QSystemTrayIcon *sysTray;
    QMenu *menuAjouter;
    QMenu *menuStopper;
    int elapsedSinceEncodingStartedMemory;
    class COptions *optDlg;
    class CRipDVD *ripDVDDlg;
    QMenu *contextMenuTable;
    QMenu *menuRetirer;
    QString whereIam;
    QAction *actionEncoder;
    QAction *actionDepending;
    QAction *actionOpenDirectory;
    QAction *actionAppend;
    QAction *actionOption;
    QAction *actionReProcess;
    QString showCommand;
    QAction *actionAddFiles;
    QAction *actionSave;
    QAction *actionripDVD;
    QAction *actionOpen;
    QAction *actionAddDir;
    QAction *actionOpenProfileDir;
    QAction *actionAddDirContents;
    QAction *actionShowApp;
    QAction *actionShow;
    QAction *actionEmptyQueue;
    QAction *actionStopAll;
    QAction *actionStop;
    QAction *actionRemoveSelected;
    QAction *actionHelp;
    QAction *actionAbout;
    QAction *actionDonate;
    QAction *actionQuit;
    QAction *actionPlay;
    QAction *actionchangeProfiles;
    QAction *actionOpenLogFile;
    QAction *actionOpenConfFile;
    bool goingToquit;
    bool popupClose;
    // Lorsque qu'un CPU i est en cours de travail on marque
    // stateCPU[i] = "locked" sinon  stateCPU[i] = ""
    QString stateCPU[MAXCPU];

    int totalDurationInQueue;
    QTime startedTime;
    int progressBarCurrentValue;
    QString lastEvent;
    bool bugResize;

public slots:
    void appTimer();
    void donate();
    void clickOnTableWidget(int, int);
    void updateProgressBar0();
    void updateProgressBar1();
    void updateProgressBar2();
    void updateProgressBar3();
    void updateProgressBar4();
    void updateProgressBar5();
    void updateProgressBar6();
    void updateProgressBar7();
    void updateProgressBar(int);
    void changeActionState();
    void finishedEncoding(int,QProcess::ExitStatus,int);
    void finishedEncoding0(int ,QProcess::ExitStatus );
    void finishedEncoding1(int ,QProcess::ExitStatus );
    void finishedEncoding2(int ,QProcess::ExitStatus );
    void finishedEncoding3(int ,QProcess::ExitStatus );
    void finishedEncoding4(int ,QProcess::ExitStatus );
    void finishedEncoding5(int ,QProcess::ExitStatus );
    void finishedEncoding6(int ,QProcess::ExitStatus );
    void finishedEncoding7(int ,QProcess::ExitStatus );

    bool readXmlFile( );
    bool readXmlFile( QString );
    bool writeXmlFile();
    bool writeXmlFile(QString);
    bool ripDVD();


private:
    Ui::kakolaClass *ui;
    void initSysTray();
    bool empty_queue();
    bool shouldWeContinu(void);
    void initTableWidget(void);
    void resizeColumnTableWidget(double, double, double);
    void reinitProgressBar(void);
    int getIndiceOfFirstFileNotYetDone(void);
    int getIndiceOFirstFileNotStartedWithProfileDiffFromX264();
    int assignThisCpuTofirstFileNotStarted(int ,int );
    int getIndiceOfFile(CVideoFile* );
    void displayBug(QString);
    bool areYouWorking(void);
    QDomElement CVideoFileToNode( QDomDocument &, CVideoFile * );
    QString getToolTip(QString ,QString ,QString ,QString , QString , QString , QString , QString , QString );

    QColor redColor;
    QColor green;
    QColor orange;
    QPixmap pixOk;
    QPixmap pixNok;

    void setIconInTableWidget(int ,int ,QPixmap, QString );

    bool areYouWorking(int);
    void welcomeTableWidget(void);
    int numberOfNotStarted();
    int getNextFreeCPU(void);
    QString secondToString(int);
    bool on_pushButtonEncoder_clicked(int);
    bool setInfoVideoFile(CVideoFile*);
    bool setState(CVideoFile*, int);
    bool setStateinList(QList<CVideoFile>*,int,int);
    bool displayInfoVideoFile(CVideoFile*);
    void addListOfFileInQueue(QStringList);

    void dragEnterEvent(QDragEnterEvent *event);
    void dropEvent(QDropEvent *event1);
    bool loadNewProfileFromFile(QString ); // C:/temp/toto.profile for example


private slots:
//    void pushButtonKillAll_clicked();
//    void pushButtonEmptyAll_clicked();
    void on_pushButtonMyQuit_clicked();
    void on_pushButtonKill_clicked();
    void on_pushButtonDependancies_clicked();
    bool stopAllProcess();
    void on_refreshLog_clicked();
    void sysTrayActivated(QSystemTrayIcon::ActivationReason);
    void on_deleteProfile_clicked();
    void tableWidgetItemSelectionChanged();
    void onActionOpen_directory_triggered();
    void onActionDepending();
    void onActionReProcess_triggered();
    void onActionShowApp();
    void onHelp();
    void onAbout();
    void checkUpdate();
    void updateProgressBarTableWidget(CVideoFile*,int,double);
    bool displayInfoVideoFile(int,int);
    void on_pushButtonStopper_clicked();
    void on_newProfile_clicked();
    void on_tabProfileEdit_textChanged();
    void on_saveProfile_clicked();
    void onActionQuitter_triggered();
    void onActionEmptyQueue_triggered();
    void on_buttonAjouter_clicked();
    void on_buttonRetirer_clicked();
    void on_choisirUnFichierDeLogComboBox_activated(int index);
    void onActionOptionsTriggered();
    void onActionOpenLogFileTriggered();
    void onactionOpenProfileDirTriggered();
    void onActionOpenConfFileTriggered();
    void on_tableWidget_customContextMenuRequested(QPoint pos);
    //int ToutRetirer_clicked(QString,QString);
    int ToutRetirer_clicked();
    int onActionStopAll_triggered();
    void onActionStop_triggered();
    void Retirer_clicked();
    void playSelectFile();
    void playSelectFile(QString);
    void openOutputDir();
    void Ajouter_clicked();
    void changeProfiles();
    void AjouterDirectoryContents_clicked();
    void AjouterDirectory_clicked();
    void onactionAppend();
    void onactionShow();
    bool on_pushButtonEncoder_clicked();
    void updateProfileTab(int);
    void textEditChanged();

};

#endif // kakola_H
